import Vue from 'vue'
const funcs = {
    install(Vue){
        Vue.prototype.funcs = {
        	injectScript:(src, attrs = {}) => {
                const s = document.createElement('script');
                s.type = 'text/JavaScript';
                s.async = true;
                s.src = src;
                Object.getOwnPropertyNames(attrs).forEach(function (key) {
                    s[key] = attrs[key];
                })
                const t = document.getElementsByTagName('script')[0];
                t.parentNode.insertBefore(s, t);
            },
            getOS:() => {
                var sUserAgent = navigator.userAgent;
                var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
                var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
                if (isMac) return "Mac";
                var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
                if (isUnix) return "Unix";
                var isLinux = (String(navigator.platform).indexOf("Linux") > -1);
                if (isLinux) return "Linux";
                if (isWin) {
                    var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1;
                    if (isWin2K) return "Win2000";
                    var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1;
                    if (isWinXP) return "WinXP";
                    var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1;
                    if (isWin2003) return "Win2003";
                    var isWinVista = sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1;
                    if (isWinVista) return "WinVista";
                    var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1;
                    if (isWin7) return "Win7";
                    var isWin10 = sUserAgent.indexOf("Windows NT 10") > -1 || sUserAgent.indexOf("Windows 10") > -1;
                    if (isWin10) return "Win10";
                }
                return "other";
            },
            getExplore:() => {
                var sys = {},
                    ua = navigator.userAgent.toLowerCase(),
                    s;
                (s = ua.match(/rv:([\d.]+)\) like gecko/)) ? sys.ie = s[1] :
                    (s = ua.match(/msie ([\d\.]+)/)) ? sys.ie = s[1] :
                        (s = ua.match(/edge\/([\d\.]+)/)) ? sys.edge = s[1] :
                            (s = ua.match(/firefox\/([\d\.]+)/)) ? sys.firefox = s[1] :
                                (s = ua.match(/(?:opera|opr).([\d\.]+)/)) ? sys.opera = s[1] :
                                    (s = ua.match(/chrome\/([\d\.]+)/)) ? sys.chrome = s[1] :
                                        (s = ua.match(/version\/([\d\.]+).*safari/)) ? sys.safari = s[1] : 0;

                // 根据关系进行判断
                if (sys.ie) return ('IE ' + sys.ie);
                if (sys.edge) return ('EDGE ' + sys.edge);
                if (sys.firefox) return ('Firefox ' + sys.firefox);
                if (sys.chrome) return ('Chrome ' + sys.chrome);
                if (sys.opera) return ('Opera ' + sys.opera);
                if (sys.safari) return ('Safari ' + sys.safari);
                return 'Unkonwn';
            },
            formatNumber:(num)=>{
                return num >= 1e3 && num < 1e4 ? (num / 1e3).toFixed(1) + 'k' : num >= 1e4 ? (num / 1e4).toFixed(1) + 'w' : num
            }
        };
        
    }
}
Vue.use(funcs);